package com.dzpk.gameserver.util;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class EnumIdChecker {
    /**
     *遍历包 并遍历子包
     */
    public static void scan(String packageName, List<Class<?>> list) throws Exception {
        String path = getSrcPath() + packageToDir(packageName);
        File dir = new File(path);
        File[] files = dir.listFiles();
        Class<?> cla = null;
        for (File f : files) {
            if (f.isDirectory()) {
                String childName = packageName + "." + f.getName();
                scan(childName, list);

            } else {
                cla = Class.forName(packageName + "." + f.getName().split("\\.")[0]);
                list.add(cla);
            }
        }
    }

    /**
     * 获取当前路径
     */
    public static String getSrcPath() throws IOException {
        File file = new File("");
        String path = file.getCanonicalPath() + File.separator + "src";
        return path;
    }

    /**
     *package转换为路径格式
     */
    public static String packageToDir(String packageName) {
        String[] array = packageName.split("\\.");
        StringBuffer sb = new StringBuffer();
        for (String str : array) {
            sb.append(File.separator).append(str);
        }
        return sb.toString();
    }

    public static void main(String[] args) throws Exception {
        List<Class<?>> list = new ArrayList<Class<?>>();
        scan("com", list);
        System.out.println(list.size());
        for (Class<?> cla : list) {
            if (cla.isEnum()) {
                check_id(cla, "getId");
                check_id(cla, "getType");
                check_id(cla, "getValue");
                check_id(cla, "getFunc");

                // if (method != null) {
                // } else {
                // method = cla.getMethod("type");
                // if (method != null) {
                //
                // } else {
                //
                // }
                // }
                // if (method == null) {
                // method = cla.getMethod("value");
                // }
                // if (method == null) {
                //
                // method = cla.getMethod("func");
                // }
                // if (method == null) {
                // System.err.println(String.format("%s 没有枚举获取方法", cla.getName()));
                // return;
                // }

            }

            // System.out.println(cla.getName());
        }
    }

    private static void check_id(Class<?> cla, String methodName) throws NoSuchMethodException, IllegalAccessException,
            InvocationTargetException {
        Method method = cla.getMethod("values");
        Object[] ids = (Object[]) method.invoke(null, null);
        Set<Integer> checkSet = new HashSet<Integer>();
        // 枚举Enum遍历值
        for (Object id : ids) {
            try {
                Method idMe = id.getClass().getMethod(methodName);
                // Method[] idMe = id.getClass().getMethods();
                Integer idValue = (Integer) (idMe.invoke(id, null));
                if (checkSet.contains(idValue)) {
                    System.err.println(String.format("%s 的  ID: %s 重复 ...... ", cla.getName(), idValue));
                }
                checkSet.add(idValue);
            } catch (Exception e) {
                // e.printStackTrace();
            }
        }
        checkSet.clear();
        checkSet = null;
    }
}
